/*  Mash-code. Mash Arts & Métiers Angers embedded software
 *  Copyright (C) 2014  Mash Arts & Métiers Angers
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/* 
 *  Main Arduino v0.1
 *
 * 
 *  Fonctionalitées : -Read/Write I2C compatible avec le contoleur de vitesse
 *                    -Retrour via port series
 * 
 */


#include <Wire.h>

uint8_t niveaux=0;

void setup()
{
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
}

void loop()
{
  niveaux++;
  
  writeI2C(0x50, 0x01, niveaux);
  
  Serial.print("Moteur ON [");
  Serial.print(niveaux*100/255);
  Serial.println("%]");
  
//  if( Serial.available() )
//  {
//    Serial.read();
//    if(readI2C(0x50, 0x01) > 10)
//    {
//      Serial.print("Moteur ON [");
//      Serial.print(niveaux*100/255);
//      Serial.println("%]");
//      writeI2C(0x50, 0x01, niveaux);
//    }
//    else
//    {
//      Serial.println("Moteur OFF");
//      writeI2C(0x50, 0x01, 0x00);
//    }
//  }
  
  delay(100);
}

int readI2C(int adrDevice, int adrReg) {
  //start the communication with IC with the address xx
  Wire.beginTransmission(adrDevice); 
  //send a bit and ask for register zero
  Wire.write(adrReg);
  //end transmission
  Wire.endTransmission();
  //request 1 byte from address xx
  Wire.requestFrom(adrDevice, 1);
  //wait for response
  while(Wire.available() == 0);
  //put the temperature in variable c
  int c = Wire.read();   
  return c;
}

int writeI2C(int adrDevice, int adrReg, uint8_t data) {
  Wire.beginTransmission(adrDevice);
  Wire.write(adrReg);
  Wire.write(data);
  Wire.endTransmission();
}
